package com.buba.system.controller;

import com.buba.system.pojo.Employee;
import com.buba.system.pojo.Page;
import com.buba.system.service.EmployeeService;
import com.buba.system.service.JobService;
import com.buba.system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/Employee")
@RestController
public class EmpController {
    @Autowired
    @Qualifier("employeeServiceImpl")
    private EmployeeService employeeService;
    @Autowired
    @Qualifier("jobServiceImpl")
    private JobService jobService;
    @Autowired
    @Qualifier("loginServiceImpl")
    private LoginService loginService;
    @RequestMapping("/selEmp")
    public Map<String,Object> selEmp(Integer pageNow,Integer pageCount,String employeeName,String jobTitle1){
        Map<String,Object> map = new HashMap<>();
        Page page = new Page(pageNow,pageCount);
//        //查询职位信息
//        List<Job> joblist=jobService.selJobAll();
        map.put("employeeName",employeeName);
        map.put("jobTitle1",jobTitle1);
        // 查询总条数
        long count = employeeService.selEmpCount(map);
        page.computePageTotal(count);
        map.put("page",page);
        //携带分页条件进行查询
        List<Employee> empList = employeeService.selEmp(map);
        empList.forEach(e -> {
            e.setJobList(jobService.selEmpTitle(e.getEmployeeId()));
        });
        // 把查询结果装入Map中
        map.put("list",empList);
        return map;
    }

    //添加方法
    @RequestMapping("/addEmp")
    public Map<String,Object> addEmp(Employee employee){
        Map<String,Object> map1 = new HashMap<>();
        Long sj= System.currentTimeMillis();
        String a = sj+"";
        employee.setEmployeeId(a);
        String str=employee.getJobTitle();
        String[] chrstr=str.split(",");
        //添加员工信息
        Integer count=employeeService.addEmp(employee);
        //添加职位信息
        for(int i=0;i<chrstr.length;i++)
        {
            Map<String,Object> map = new HashMap<>();
            map.put("jobId",chrstr[i]);
            map.put("employeeId",employee.getEmployeeId());
            Integer count1=employeeService.addEmpjob(map);
        }
        //添加登录账号信息
        String employyIdCard=employee.getEmployeeIdCard().toString();
        map1.put("password",employyIdCard.substring(12));
        map1.put("employeeIdCard",employee.getEmployeeIdCard());
        map1.put("employeeId",employee.getEmployeeId());
        Integer count2=loginService.addLogin(map1);
        if(count!=null){
            map1.put("message","添加成功");
        }
        else{
            map1.put("message","添加失败");
        }
        return map1;
    }
    //删除方法
    @RequestMapping("/delEmp")
    public Map<String,Object> delEmp(String employeeId){
        Map<String,Object> map = new HashMap<>();
        //删除职位中间表
        Integer count=jobService.delJob(employeeId);
        //删除登录信息表
        Integer count1=loginService.delLogin(employeeId);
        //删除员工信息
        Integer count2=employeeService.delEmp(employeeId);
        if(count!=null){
            map.put("message","删除成功");
        }
        return map;
    }
    @RequestMapping("/delEmpAll")
    public Map<String,Object> delAllBook(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        //删除职位中间表
        Integer count=jobService.delJobAll(list);
        //删除登录信息表
        Integer count1=loginService.delLoginAll(list);
        //删除员工信息
        Integer count2=employeeService.delEmpAll(list);
        if(count2!=null){
            map.put("message","删除"+count2+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }
    //修改方法
    @RequestMapping("/updEmp")
    public Map<String,Object> updEmp(@RequestParam Map<String, Object> map, String[] jobId){

        map.put("jobIds",jobId);

        //先删除所有的职位
        Integer count=jobService.delJob(map.get("employeeId").toString());
        //修改员工信息
        Integer count1=employeeService.updEmp(map);
        //添加职位信息
        Integer count2=jobService.addJob(map);
        if(count1!=0){
            map.put("message","修改成功");
        }else{
            map.put("message","修改失败");
        }
        return map;
    }

}
